/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.pneu.pneuservisbackend.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import pa165.pneu.pneuservisbackend.entiies.Customer;
import pa165.pneu.pneuservisbackend.entiies.OrderPneu;
import pa165.pneu.pneuservisbackend.entiies.ServicePneu;

/**
 *
 * @author Martin Tarasovic
 */
@Repository
public class OrderPneuDAOImpl implements OrderPneuDAO {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void createOrder(OrderPneu orderPneu) {
        if (orderPneu == null || orderPneu.getId() != null) {
            throw new IllegalArgumentException("order penu is null or id is set");
        }
        if (orderPneu.getCustomer() == null || orderPneu.getServicePneus() == null || orderPneu.getStatus() == null || orderPneu.getVehicleBrand() == null) {
            throw new IllegalArgumentException("some parameters are missing" + orderPneu);
        }
        
        List<ServicePneu> merged = new ArrayList<ServicePneu>();
        orderPneu.setServicePneus(merged);
        entityManager.persist(orderPneu);
        for(ServicePneu servicePneu : orderPneu.getServicePneus()) {
            merged.add(entityManager.find(ServicePneu.class,servicePneu.getId()));
        }
        
        orderPneu.setServicePneus(merged);
        entityManager.detach(orderPneu);

    }

    @Override
    public OrderPneu getOrder(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id is null");
        }
        
        OrderPneu order = entityManager.find(OrderPneu.class, id);
        entityManager.detach(order);
        return order;
    }

    @Override
    public void updateOrder(OrderPneu orderPneu) {
        validate(orderPneu);
        
        OrderPneu orderFromDatabase = entityManager.find(OrderPneu.class, orderPneu.getId());
    
        if (orderFromDatabase == null) {
            throw new IllegalArgumentException("order is not in database" + orderPneu);
        }

        entityManager.merge(orderPneu);
        entityManager.detach(orderPneu);

 }

    @Override
    public void removeOrder(OrderPneu orderPneu) {
        if (orderPneu == null || orderPneu.getId() == null) {
            throw new IllegalArgumentException("orderPneu or id is null");
        }
        
        OrderPneu result = entityManager.find(OrderPneu.class, orderPneu.getId());
        
        if (result == null) {
            throw new IllegalArgumentException("order is not in databse");
        }
        
        entityManager.remove(result);
        
    }

    @Override
    public List<OrderPneu> getAllOrders() {
        
        List<OrderPneu> orders = entityManager.createQuery("SELECT o FROM OrderPneu o").getResultList();
        
        return orders;
    }

    @Override
    public List<OrderPneu> getOrdersByCustomer(Customer customer) {
        if (customer == null || customer.getId() == null) {
            throw new IllegalArgumentException("customer or customer id is null");
        }
        OrderPneu order = new OrderPneu();
        
        Query query = entityManager.createQuery("SELECT o FROM OrderPneu o WHERE o.customer = :cid ");
        query.setParameter("cid", customer);
        List<OrderPneu> orders = query.getResultList();
        entityManager.detach(orders);
        return orders;
    }
    
    private void validate(OrderPneu order) {
        if (order == null) {
            throw new IllegalArgumentException("orderPneu is null or it is not in databse");
        }
        if (order.getCustomer() == null) {
            throw new IllegalArgumentException("customer is null");
        }
        if (order.getCustomer().getId() == null) {
            throw new IllegalArgumentException("customer in order does not exist in database");
        }
    }
}
